package one.stand.controller.supplier;

import com.jmp.api.dto.DynamicMyListResponse;
import com.jmp.api.dto.DynamicMyRemoveRequest;
import com.jmp.api.dto.DynamicMySaveRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.DynamicListDto;
import com.jmp.base.dto.DynamicListVo;
import com.jmp.base.dto.DynamicRemoveDto;
import com.jmp.base.dto.DynamicSaveRequest;
import com.jmp.feign.DynamicFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.CommentDto;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageRequest;
import one.stand.dto.PageResponse2;
import one.stand.model.ResultModel;
import one.stand.service.RecordPublicService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "我的动态")
@RequestMapping("dynamic/my")
@RestController
public class DynamicMyController {
    @Autowired
    private DynamicFeign dynamicFeign;

    @Autowired
    private RecordPublicService recordPublicService;

    @ApiOperation(value = "1我的动态列表", notes = "点赞、回复、分享等接口 请查看【首页-红木圈】下相关接口")
    @GetMapping("/list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<DynamicMyListResponse> list(@RequestAttribute PageRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        DynamicListDto dynamicListDto = DynamicListDto.builder()
                .userId(optUser.getUserId())
                .build();
        dynamicListDto.setPageNum(request.getPageNum());
        dynamicListDto.setPageSize(request.getPageSize());
        ResultModel<DynamicListVo> resultModel = dynamicFeign.list(dynamicListDto);
        AssertUtil.isTrue(resultModel);

        PageResponse2<DynamicMyListResponse.Dynamic> dynamicPageResponse2 = ConvertUtils.page2(resultModel.getData().getDynamics(), dynamic -> {

            //最早三条评论
            List<CommentDto> commentDtos = recordPublicService.getCommentList(dynamic.getDynamicId(), 3);
            List<DynamicMyListResponse.Comment> commentResponseList = ConvertUtils.list(commentDtos, commentModel -> {
                return DynamicMyListResponse.Comment.builder()
                        .userName(commentModel.getUserName())
                        .content(commentModel.getText())
                        .build();
            });

            boolean isThumbs = recordPublicService.isThumbs(optUser.getUserId(), RecordPublicService.THUMBS_TYPE_ARTICLE, dynamic.getDynamicId());

            return DynamicMyListResponse.Dynamic.builder()
                    .dynamicId(dynamic.getDynamicId())

                    .time(dynamic.getTime())
                    .type(dynamic.getType())
                    .title(dynamic.getTitle())
                    .photos(dynamic.getPhotos())

                    .commentNum(recordPublicService.countCommentByDynamic(dynamic.getDynamicId()))
                    .doThumbsNum(recordPublicService.getThumbsCount(RecordPublicService.THUMBS_TYPE_ARTICLE, dynamic.getDynamicId()))
                    .thumbsFlag(BooleanUtils.toInteger(isThumbs))

                    .commentList(commentResponseList)
                    .build();
        });

        DynamicMyListResponse response = DynamicMyListResponse.builder()
                .dynamics(dynamicPageResponse2)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "2发布")
    @PostMapping("/save")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel save(@RequestAttribute DynamicMySaveRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        ResultModel resultModel = dynamicFeign.save(DynamicSaveRequest.builder()
                .companyId(optUser.getCompanyId())
                .createUserId(optUser.getUserId())
                .type(request.getType())
                .title(request.getTitle())
                .photos(request.getPhotos())
                .details(request.getDetails())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "2删除")
    @PostMapping("/remove")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel remove(@RequestAttribute DynamicMyRemoveRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        ResultModel resultModel = dynamicFeign.remove(DynamicRemoveDto.builder()
                .dynamicId(request.getDynamicId())
                .optUserId(optUser.getUserId())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }
}
